// packageC/selfCarry/writeOffCodePage/writeOffCodePage.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectDeliverId: null,
    showDeliverList: false,
    deliverList: [],//当前账号所属自提点
    list: [],
    used: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        let info_arr = [];
        info_arr = scene.split(',');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          if (chil_arr[0] == "id") {
            this.data.id = chil_arr[1];
          } else if (chil_arr[0] == "coupon_id") {
            this.data.coupon_id = chil_arr[1];
          }
        }
      }
    }
    this.getDeliverList();
    // this.getData();
  },
  getDeliverList() {
    let urlStr = app.getNetAddresss('plugin.package-deliver-write-off.frontend.controllers.write-off.clerk-delivers');
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        let data = res.data;
        if (res.result == 1) {
          this.setData({
            selectDeliverId: data.list[0].id,
            deliverList: data.list,
            showDeliverList: true
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  selectDeliverItem(e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      selectDeliverId: id
    });
  },
  selectDeliver() {
    this.setData({
      showDeliverList: false
    });
    this.getData();
  },
  getData() {
    let _parame = {
      id: this.data.selectDeliverId,
      coupon_id: this.data.coupon_id
    };

    let urlStr = app.getNetAddresss('plugin.package-deliver-write-off.frontend.controllers.write-off.write-off-page');
    app._postNetWork({
      url: urlStr,
      data: _parame,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            list: res.data.info,
            used: res.data.used
          });
          if(res.data.used) {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: '该优惠券已核销过'
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  onClick(){
    let _parame = {
      id: this.data.selectDeliverId,
      coupon_id: this.data.coupon_id
    };
    let urlStr = app.getNetAddresss('plugin.package-deliver-write-off.frontend.controllers.write-off.write-off');
    app._postNetWork({
      url: urlStr,
      data: _parame,
      success: (resdata) => {
        let res = resdata.data;
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: res.msg
        });
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})